import {storage} from '@/common/utils/storage.js'
import {count} from '@/common/utils/count.js'
//常用的工具方法类
export const tools = {
	//CHN=miguan&BT=1&DN=MG03001002类型的参数转对象
	 paramsToObj(params) {
		var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
		var obj = {};
		while (reg.exec(params)) {
			obj[RegExp.$1] = RegExp.$2;
		}
		return obj;
	},
	//https://ky.railshj.cn?CHN=miguan&BT=1&DN=MG03001002带url的类型的参数转对象
	urlParmasToObj(url) {
		var reg_url = /^[^\?]+\?([\w\W]+)$/,
				reg_para = /([^&=]+)=([\w\W]*?)(&|$)/g, //g is very important
				arr_url = reg_url.exec(url),
				ret = {};
		if (arr_url && arr_url[1]) {
			var str_para = arr_url[1], result;
			while ((result = reg_para.exec(str_para)) != null) {
				ret[result[1]] = result[2];
			}
		}
		return ret;
	},
	//获取从第m天开始之后的n天的时间
	getAfterDays(m, n){
		var nowDate = new Date();
		var startDate = new Date();
		var endDate = new Date();
		startDate.setDate(nowDate.getDate() + m)
		endDate.setDate(nowDate.getDate() + m + n - 1);
		
		var dateArr = [];
		while((endDate.getTime() - startDate.getTime()) >= 0) {
			var year = startDate.getFullYear()
			var month = (startDate.getMonth() + 1).toString().length == 1 ? "0" + (startDate.getMonth() + 1).toString() : (startDate.getMonth() + 1);
			var day = startDate.getDate().toString().length == 1 ? "0" + startDate.getDate() : startDate.getDate();
			dateArr.push(month + "月" + day + "日");
			startDate.setDate(startDate.getDate() + 1);
		}
		return dateArr
	},
	//获取从第m天开始之后的n天的时间
	getAfterDays_y_m_d(m, n){
		var nowDate = new Date();
		var startDate = new Date();
		var endDate = new Date();
		startDate.setDate(nowDate.getDate() + m)
		endDate.setDate(nowDate.getDate() + m + n - 1);
		
		var dateArr = [];
		while((endDate.getTime() - startDate.getTime()) >= 0) {
			var year = startDate.getFullYear()
			var month = (startDate.getMonth() + 1).toString().length == 1 ? "0" + (startDate.getMonth() + 1).toString() : (startDate.getMonth() + 1);
			var day = startDate.getDate().toString().length == 1 ? "0" + startDate.getDate() : startDate.getDate();
			dateArr.push(year + "-" + month + "-" + day)
			startDate.setDate(startDate.getDate() + 1);
		}
		return dateArr
	},
	getTimes(arg1, arg2){
		let times = []
		for(var i=parseInt(arg1.split(':')[0]) ; i<=parseInt(arg2.split(':')[0]); i++){
			var hour = ''
			hour = i < 10 ?  '0'+i : i
			times.push(hour+':00')
			times.push(hour+':30')
		}
		if(parseInt(arg1.split(':')[1]) >= 30){
			times.shift()//删除第一个元素
		}
		if(parseInt(arg2.split(':')[1]) == 0){
			times.splice(times.length-1,1)
		}
		let data = []
		for(var i = 0; i < times.length-1; i++){
			data.push(times[i]+'-'+times[i+1])
		}
		return data
	},
	getTimes_detail(arg1, arg2){
		let times = []
		for(var i=parseInt(arg1.split(':')[0]) ; i<=parseInt(arg2.split(':')[0]); i++){
			var hour = ''
			hour = i < 10 ?  '0'+i : i
			times.push(hour+':00')
			times.push(hour+':30')
		}
		if(parseInt(arg1.split(':')[1]) >= 30){
			times.shift()//删除第一个元素
		}
		if(parseInt(arg2.split(':')[1]) == 0){
			times.splice(times.length-1,1)
		}
		let data = []
		for(var i = 0; i < times.length-1; i++){
			data.push(times[i]+' - '+times[i+1])
		}
		return data
	},
	//根据经纬度计算最近的车站
	getRecentlyStation(stationList, latitude, longitude){
		var min = 0
		var minItem = stationList[0]
		for(var i in stationList){
			var item = stationList[i]
			// 经纬度为0,不加入计算
			if(item.latitude == 0 && item.longitude == 0){
				continue
			}
			var a = Number(count.sub(item.latitude, latitude))
			var b = Number(count.sub(item.longitude, longitude))
			var c = Math.sqrt(a*a+b*b);
			if(i == 0){
				min = c
			}
			if(c < min){
				min = c
				minItem = item
			}
		}
		return minItem
	},
}